﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ACP.Business;
using ACP.DTO;

namespace ACP.Web.UI
{
    public partial class Result : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["testId"] != null)
                {
                    int testId = Convert.ToInt32(Session["testId"]);
                    BindResults(testId);
                }
            }
        }

        private List<tempexam> GetResults(int testId)
        {
            if (Session["ExamQuestionAndAnswers"] != null)
                return (List<tempexam>)Session["ExamQuestionAndAnswers"];
            else
            return TempExamLogic.GetExamQuestionAndAnswersByTestId(testId);
        }
        private void BindResults(int testId)
        {
            try
            {
                result resultObj = ResultLogic.GetResultsWithAssociations(testId);
                List<tempexam> lstQuestions = GetResults(testId);
                if (resultObj != null)
                {
                    double dblTimeUtilized = Convert.ToDouble(resultObj.timeutilized);
                    string strTestDuration = dblTimeUtilized > 60 ? Convert.ToString(Math.Round(dblTimeUtilized / 60,2)) + " Minitues" : dblTimeUtilized > 3600 ? Convert.ToString(Math.Round(dblTimeUtilized / 3600,2)) + " Hours" : Convert.ToString(Math.Round(dblTimeUtilized,2)) + " Seconds";
                    StringBuilder sb = new StringBuilder();
                    sb.Append("Results for: " + resultObj.user.firstname + " " + resultObj.user.lastname);
                    sb.Append("<hr>");
                    sb.Append("<strong>User Name:</strong> " + resultObj.user.firstname + " " + resultObj.user.lastname);
                    sb.Append("<br /><strong>Score:</strong> " + resultObj.marks + " out of " + resultObj.examtype.noofquestions);
                    sb.Append("<br /><strong>Percentage:</strong> " + resultObj.precentage + "%");
                    sb.Append("<br /><strong>Date started:</strong> " + resultObj.dateofstarttest.ToString());
                    sb.Append("<br /><strong>Date finished:</strong> " + resultObj.dateofendtest.ToString());
                    sb.Append("<br /><strong>Duration:</strong> " + strTestDuration);
                    sb.Append("<br />");
                    sb.Append("<hr>");
                     ltrlUserInfo.Text = sb.ToString();
                    sb.Clear();

                    if(lstQuestions != null)
                    {
                        if (lstQuestions.Count(x => x.SelectedAnswer == "") > 0)
                        {
                            ltrlReviewTA.Text = "<br /><strong>Total Answered:</strong> " + lstQuestions.Count().ToString();
                            pnlReviewTA.Visible = true;
                        }
                        if (lstQuestions.Count(x => (bool)x.IsResult) > 0)
                        {
                            ltrlReviewCA.Text = "<br /><strong>Correct Answers:</strong> " + lstQuestions.Count(x => (bool)x.IsResult).ToString();
                            pnlReviewCA.Visible = true;
                        }
                        if (lstQuestions.Count(x => !(bool)x.IsResult) > 0)
                        {
                            ltrlReviewWA.Text = "<br /><strong>Wrong Answers:</strong> " + lstQuestions.Count(x => !(bool)x.IsResult).ToString();
                            pnlReviewWA.Visible = true;
                        }
                       
                        //Session["ReviewQuestionAnswers"] = lstQuestions;
                        
                    }
                    

                }
            }
            catch (Exception ex)
            {
                acptest_ErrorLog errorLog = new acptest_ErrorLog()
                {
                    //ex.Message, "Page_Load", "Test Page"
                    ErrorMessage = ex.Message,
                    ErrorFunction = "BindResults()",
                    InsertedTime = DateTime.Now,
                    ModuleName = "Result.aspx"
                };
                ACPErrorLogLogic.ErrorLog(errorLog);
            }
           
        }

        private void BindReview(string linkId)
        {
            try
            {
                int testId = Session["testId"] != null ? Convert.ToInt32(Session["testId"]) : 0;
                string strReviewType = string.Empty;
                List<tempexam> lstExamQuestions = GetResults(testId);
                switch (linkId)
                {
                    case "lnkCA":
                        lstExamQuestions = lstExamQuestions.Where(x => (bool)x.IsResult).ToList();
                        strReviewType = "Correct Answered Questions Review";
                        break;
                    case "lnkWA":
                        lstExamQuestions = lstExamQuestions.Where(x => !(bool)x.IsResult).ToList();
                        strReviewType = "Wrong Answered Questions Review";
                        break;
                    default:
                        strReviewType = "Total Answered Questions Review";
                        break;
                }
                
                StringBuilder sb = new StringBuilder();
                sb.Append("<hr>");
                sb.Append("<strong>" +strReviewType + "</strong>");
                sb.Append("<hr>");
                int questionNo = 0;
                foreach (var item in lstExamQuestions)
                {
                    questionNo++;
                    sb.Append("<hr>");
                    sb.Append("<br />");
                    sb.Append(questionNo.ToString() + ") " + item.question.question1);
                    sb.Append("<br />");
                    sb.Append("A) " + item.question.option1);
                    sb.Append("<br />");
                    sb.Append("B) " + item.question.option2);
                    sb.Append("<br />");
                    if (item.question.option3 != null) sb.Append("C) " + item.question.option3); sb.Append("<br />");
                    if (item.question.option4 != null) sb.Append("D) " + item.question.option4); sb.Append("<br />");
                    if (item.question.option5 != null) sb.Append("E) " + item.question.option5); sb.Append("<br />");
                    sb.Append("<br /><strong>Your Answer:</strong> " + item.SelectedAnswer);
                    sb.Append("<br />");
                    sb.Append("<br /><strong>Correct Answer:</strong> " + item.OriginalAnswer);
                    sb.Append("<br />");
                    sb.Append("<br /><strong>Explanation:</strong> " + item.question.description);
                    sb.Append("<br />");
                    sb.Append("<hr>");
                }
                ltrlResult.Text = sb.ToString();
                pnlReview.Visible = true;
            }
            catch (Exception ex)
            {
                acptest_ErrorLog errorLog = new acptest_ErrorLog()
                {
                    //ex.Message, "Page_Load", "Test Page"
                    ErrorMessage = ex.Message,
                    ErrorFunction = "BindReview",
                    InsertedTime = DateTime.Now,
                    ModuleName = "Result.aspx"
                };
                ACPErrorLogLogic.ErrorLog(errorLog);
            }
           
        }

        protected void lnkCA_Click(object sender, EventArgs e)
        {
            try
            {
                string linkId = ((LinkButton)sender).ID;
                BindReview(linkId);
            }
            catch (Exception ex)
            {
                acptest_ErrorLog errorLog = new acptest_ErrorLog()
                {
                    //ex.Message, "Page_Load", "Test Page"
                    ErrorMessage = ex.Message,
                    ErrorFunction = "lnkReviewCA_Click",
                    InsertedTime = DateTime.Now,
                    ModuleName = "Result.aspx"
                };
                ACPErrorLogLogic.ErrorLog(errorLog);
            }
        }

        protected void lnkWA_Click(object sender, EventArgs e)
        {
            try
            {
                string linkId = ((LinkButton)sender).ID;
                BindReview(linkId);
            }
            catch (Exception ex)
            {
                acptest_ErrorLog errorLog = new acptest_ErrorLog()
                {
                    //ex.Message, "Page_Load", "Test Page"
                    ErrorMessage = ex.Message,
                    ErrorFunction = "lnkReviewWA_Click",
                    InsertedTime = DateTime.Now,
                    ModuleName = "Result.aspx"
                };
                ACPErrorLogLogic.ErrorLog(errorLog);
            }
        }

        protected void lnkNA_Click(object sender, EventArgs e)
        {
            try
            {
                string linkId = ((LinkButton)sender).ID;
                BindReview(linkId);
            }
            catch (Exception ex)
            {
                acptest_ErrorLog errorLog = new acptest_ErrorLog()
                {
                    //ex.Message, "Page_Load", "Test Page"
                    ErrorMessage = ex.Message,
                    ErrorFunction = "lnkReview_Click",
                    ModuleName = "Result.aspx"
                };
                ACPErrorLogLogic.ErrorLog(errorLog);
            }
        }
    }
}